package com.ruoyi.web.controller.ccc;

import java.io.File;
import java.util.List;
import java.util.Objects;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.web.controller.tool.WordToPdf;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ConferenceFiles;
import com.ruoyi.system.service.IConferenceFilesService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 会议文件Controller
 * 
 * @author ruoyi
 * @date 2024-06-13
 */
@Controller
@RequestMapping("/system/files")
public class ConferenceFilesController extends BaseController
{
    private String prefix = "system/files";

    @Autowired
    private IConferenceFilesService conferenceFilesService;

    @RequiresPermissions("system:files:view")
    @GetMapping()
    public String files(Integer conId,ModelMap modelMap)
    {
        System.out.println(conId);
        modelMap.put("conId",conId);
        return prefix + "/files";
    }

    /**
     * 查询会议文件列表
     */
    @RequiresPermissions("system:files:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ConferenceFiles conferenceFiles)
    {
        //解决逗号问题
        conferenceFiles.setConId(conferenceFiles.getConId().replace(",",""));
        //解决会议id为空
        if (Objects.equals(conferenceFiles.getConId(),"null")){
            conferenceFiles.setConId("");
        }
        startPage();
        List<ConferenceFiles> list = conferenceFilesService.selectConferenceFilesList(conferenceFiles);
        return getDataTable(list);
    }

    /**
     * 导出会议文件列表
     */
    @RequiresPermissions("system:files:export")
    @Log(title = "会议文件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ConferenceFiles conferenceFiles)
    {
        List<ConferenceFiles> list = conferenceFilesService.selectConferenceFilesList(conferenceFiles);
        ExcelUtil<ConferenceFiles> util = new ExcelUtil<ConferenceFiles>(ConferenceFiles.class);
        return util.exportExcel(list, "会议文件数据");
    }

    /**
     * 新增会议文件
     */
    @GetMapping("/add")
    public String add(Integer conId,ModelMap modelMap)
    {
        modelMap.put("conId",conId);
        return prefix + "/add";
    }

    /**
     * 新增保存会议文件
     */
    @RequiresPermissions("system:files:add")
    @Log(title = "会议文件", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Integer conId, String file,String fileName)
    {
        String filePath = RuoYiConfig.getUploadPath();//获取项目文件存放路径
        String newFilePath = file.replace("/profile/upload", filePath); //更换文件存放路径
        // 获取文件后缀
        String[] split = file.split("\\.");
        String fileSuffix = split[split.length - 1];
        String outPath = file;
        if (Objects.equals(fileSuffix,"doc")||Objects.equals(fileSuffix, "docx")) {
            outPath = WordToPdf.WordToPdf(newFilePath).replace(filePath, "/profile/upload"); //更换文件存放路径
        }
        ConferenceFiles conferenceFiles = new ConferenceFiles();
        conferenceFiles.setConfileType(fileSuffix);
        conferenceFiles.setConId(String.valueOf(conId));
        conferenceFiles.setConfileName(fileName);
        conferenceFiles.setConfileUrl(outPath);
        return toAjax(conferenceFilesService.insertConferenceFiles(conferenceFiles));
    }

    /**
     * 修改会议文件
     */
    @RequiresPermissions("system:files:edit")
    @GetMapping("/edit/{confileId}")
    public String edit(@PathVariable("confileId") Long confileId, ModelMap mmap)
    {
        ConferenceFiles conferenceFiles = conferenceFilesService.selectConferenceFilesByConfileId(confileId);
        mmap.put("conferenceFiles", conferenceFiles);
        return prefix + "/edit";
    }

    /**
     * 修改保存会议文件
     */
    @RequiresPermissions("system:files:edit")
    @Log(title = "会议文件", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ConferenceFiles conferenceFiles)
    {
        return toAjax(conferenceFilesService.updateConferenceFiles(conferenceFiles));
    }

    /**
     * 删除会议文件
     */
    @RequiresPermissions("system:files:remove")
    @Log(title = "会议文件", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(conferenceFilesService.deleteConferenceFilesByConfileIds(ids));
    }
}
